import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { IStrategyOptions, Strategy } from 'passport-local';
import { AuthService } from './auth.service';

@Injectable()
export class LocalForWebStrategy extends PassportStrategy(Strategy, 'local-web') {
  constructor(private readonly authService: AuthService) {
    super({
      usernameField: 'username',
      passwordField: 'password'
    } as IStrategyOptions);
  }

  async validate(username: string, password: string): Promise<any> {
    console.log('----进入')
    console.log(username, password)
    const user = await this.authService.validateVillager(username, password);
    if (!user) {
      throw new UnauthorizedException('账号或密码 出现错误!!!!');
    }
    return user;
  }
}
